﻿namespace Jhong.FunMVCBlog.Core.Data
{
    using Jhong.FunMVCBlog.Core.Entity;
    using Jhong.FunMVCBlog.Core.Enum;
    using Jhong.FunMVCBlog.Core.Infrastructure;
    using System;
    using System.Collections.Generic;

    public class TagData : BaseData
    {
        public PageContainer<Tag> GetList(int pageIndex, int itemsPerPage)
        {
            if (pageIndex < 1) pageIndex = 1;
            if (itemsPerPage < 1) itemsPerPage = 10;
            using (this._dbContext)
            {
                var items = this._dbContext.T("SELECT * FROM tag WHERE IsDelete=@0 order by CreateTime Desc LIMIT @1,@2", DeleteStatus.No, (pageIndex - 1) * itemsPerPage, itemsPerPage).QueryToEntitys<Tag>();
                var totalCount = this._dbContext.T("SELECT count(*) FROM tag WHERE IsDelete=@0", DeleteStatus.No).QueryFirstOrDefault<int>();
                var container = new PageContainer<Tag>(pageIndex, itemsPerPage, totalCount, items);
                return container;
            }
        }

        public IEnumerable<string> GetAllTagName()
        {
            using (this._dbContext)
            {
                var data = this._dbContext.T("select * from tag where IsDelete=@0", DeleteStatus.No).QueryToEntitys<Tag>();
                foreach (var d in data) yield return d.Name;
            }
        }

        public Tag GetByID(Guid id)
        {
            using (this._dbContext)
            {
                return this._dbContext.T("select * from tag where id=@0", id).QueryFirstOrDefault<Tag>();
            }
        }

        public IList<ArticleTag> GetByArticleID(Guid id)
        {
            using (this._dbContext)
            {
                return this._dbContext.T("SELECT * FROM article_tag WHERE ArticleID=@0", id).QueryToEntitys<ArticleTag>();
            }
        }

        public bool DeleteByID(Guid id)
        {
            using (this._dbContext)
            {
                this._dbContext.BeginTranslation();
                try
                {
                    this._dbContext.T("DELETE FROM article_tag WHERE TagID=@0", id).Execute();
                    this._dbContext.DeleteEntity(new Tag() { ID = id });
                    this._dbContext.Commit();
                    lock (_lock)
                    {
                        _cache = null;
                    }
                    return true;
                }
                catch
                {
                    this._dbContext.Rollback();
                    return false;
                }
            }
        }

        private IList<Tag> GetAll()
        {
            using (this._dbContext)
            {
                return this._dbContext.T("select * from tag where IsDelete=@0", DeleteStatus.No).QueryToEntitys<Tag>();
            }
        }

        private static readonly object _lock = new object();

        private static IDictionary<Guid, string> _cache;

        public IDictionary<Guid, string> Cache
        {
            get
            {
                lock (_lock)
                {
                    if (null == _cache)
                    {
                        _cache = new Dictionary<Guid, string>();
                        var data = this.GetAll();
                        foreach (var d in data)
                            _cache.Add(d.ID, d.Name);
                    }
                    return _cache;

                }
            }
        }

        public override bool Update<T>(T model)
        {
            using (this._dbContext)
            {
                lock (_lock)
                {
                    _cache = null;
                    return this._dbContext.UpdateEntity(model) > 0;
                }
            }
        }

        public override bool Insert<T>(T model)
        {
            using (this._dbContext)
            {
                lock (_lock)
                {
                    _cache = null;
                    return this._dbContext.InsertEntity(model) > 0;
                }
            }
        }


    }
}